package com.atguigu.gmall.seckill.talk;

import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.seckill.mapper.SeckillMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author jzq
 * @create 2021-07-14 19:08
 */

/**
 * 定时任务
 */
@Component
public class SeckillTask {
     @Resource
    private SeckillMapper seckillMapper;
    @Autowired
    private RedisTemplate redisTemplate;
     @Scheduled(cron = "0/30 * * * * *")
    public void seckillIntoRedisTask(){
          //计算出五个时间段
         List<Date> dateMenus = DateUtil.getDateMenus();
         for (Date dateMenu : dateMenus) {
             //开始时间
             String startTime = DateUtil.data2str(dateMenu, DateUtil.PATTERN_YYYY_MM_DDHHMM);
             //截至时间
             Date date = DateUtil.addDateHour(dateMenu, 2);
             String endTime = DateUtil.data2str(date, DateUtil.PATTERN_YYYY_MM_DDHHMM);
             //拼接条件
             LambdaQueryWrapper<SeckillGoods> wrapper = new LambdaQueryWrapper<>();
             //审核状态
             wrapper.eq(SeckillGoods::getStatus,"1");
             //剩余库存
             wrapper.gt(SeckillGoods::getStockCount,0);
             //开始时间<=time<结束时间
             wrapper.ge(SeckillGoods::getStartTime,startTime);
             wrapper.le(SeckillGoods::getEndTime,endTime);
             //查询redis，看是否有这个数据
             String key = DateUtil.data2str(dateMenu, DateUtil.PATTERN_YYYYMMDDHH);

             Set keys = redisTemplate.boundHashOps("SeckillGoods_" + key).keys();
             if (keys !=null && keys.size()>0){
                 wrapper.notIn(SeckillGoods::getId,keys);
             }
             //查询数据库
             List<SeckillGoods> seckillGoods = seckillMapper.selectList(wrapper);
             for (SeckillGoods seckillGood : seckillGoods) {
                 //存入redis
                 redisTemplate.boundHashOps("SeckillGoods_" + key).put(seckillGood.getId(),seckillGood);
                 //获取商品的数量
                 Integer stockCount = seckillGood.getStockCount();
                 //构建一个数量的长度的数组
                 String[] ids = getSeckillGoodsQueue(seckillGood.getId(), stockCount);
                 //将数组的每个元素存在list中
                 redisTemplate.boundListOps("seckillGoodsQueue_" +seckillGood.getId()).leftPushAll(ids);
                 //对库存进行记录
                 redisTemplate.boundHashOps("SeckillGoodsStockNum").increment(seckillGood.getId(),stockCount);
             }

         }
    }

    /**
     * 构建长度数组
     * @param goodsId
     * @param stockCount
     * @return
     */
    private String[] getSeckillGoodsQueue(String goodsId, Integer stockCount){
        String[] ids = new String[stockCount];
        for (int i = 0; i <stockCount ; i++) {
            ids[i] =goodsId;
        }
        return ids;
    }
}
